using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Reflection;


namespace MobileStreams
{
    /// <summary>
    /// Audio(Radio) stream or Video(TV) stream
    /// </summary>
    enum ViewTypes
    {
        Audio,
        Video
    }

    public struct msSettings
    {
        public static string sUpdateURL;
    }

    public class Channel
    {
        public int ID;
        public string ChannelName;
        public string Description;
        public string ChannelLogo;
        public string URL;
        public int Category;
        public Stream[] Streams;

        public override string ToString()
        {
            return ChannelName;
        }
    }

    public class Stream
    {
        public string URL;
        public string Description;

        // useful if Object is bound as Datasource so we can have a useful DisplayText (ComboBox)
        public override string ToString()
        {
            return Description;
        }
    }

    /// <summary>
    /// This class is here to handle everything that has to do with the XML-Files (De-/Serializing, Sorting Channels, ...)
    /// </summary>
    static public class XMLHandling
    {
        // Directory, where the Application is located
        static public string strAppDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);

        static public Channel[] getChannels(string name)
        {
            Channel[] ChannelArray;
            XmlSerializer s = new XmlSerializer(typeof(Channel[]));

            TextReader r = new StreamReader(name);
            ChannelArray = (Channel[])s.Deserialize(r);
            r.Close();

            //SortChannels(ref ChannelArray);
            //return ChannelArray;

            return ChannelArray;
        }

        static public void WriteChannels(Channel[] Canals, string filename)
        {
            XmlSerializer s = new XmlSerializer(typeof(Channel[]));

            TextWriter w = new StreamWriter(filename);
            s.Serialize(w, Canals);
            w.Close();
        }

        /// <summary>
        /// Sorts ascending by Channel.ChannelName
        /// </summary>
        /// <param name="unsorted"></param>
        static public void SortChannels(ref Channel[] unsorted)
        {
            int i, j;

            for (i = unsorted.Length - 1; i > 0; i--)
            { 
                for (j = 0; j < i; j++)
                {
                    if (string.Compare(unsorted[j].ChannelName, unsorted[j + 1].ChannelName) > 0)
                    {
                        Channel cTemp = unsorted[j];
                        //less 0 = A is less than B
                        //0 = equal
                        //> 0 = A is greater than B
                        unsorted[j] = unsorted[j + 1];
                        unsorted[j + 1] = cTemp;
                    }
                }
            }

            i = 0;
            foreach (Channel cn in unsorted)
            {
                cn.ID = i;
                i++;
            }

            //return returnChannels;
        }
    }
}
